package com.springboot.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.springboot.biz.ManholeBiz;
import com.springboot.biz.OperatorBiz;
import com.springboot.biz.PipeBiz;
import com.springboot.biz.ProjectBiz;
import com.springboot.entity.Manhole;
import com.springboot.entity.Operator;
import com.springboot.entity.Pipe;
import com.springboot.entity.Project;
import com.springboot.entity.User;
import com.springboot.util.AppUtils;

@RestController
@RequestMapping(value = "/manhole")
public class ManholeController {

    @Value("${myfile}")
    private String myfile;
    @Value("${mypath}")
    private String mypath;

    @Resource
    private OperatorBiz operatorBiz;
    @Resource
    private ProjectBiz projectBiz;
    @Resource
    private ManholeBiz manholeBiz;
    @Resource
    private PipeBiz pipeBiz;


    /**
     * 编辑数据
     */
    @RequestMapping(value = "/editinfo")
    public ModelAndView editInfo(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("common/failure");
        User user = (User) AppUtils.findMap("user");
        Manhole manhole = manholeBiz.findInfoManhole(id, user);
        if (id != 0 && StringUtils.isEmpty(manhole))
            return view;
        List<Operator> operators = operatorBiz.findListOperator(user.getCompany());
        List<Pipe> pipes = pipeBiz.findListPipe(manhole);
        view.setViewName("manhole/editinfo");
        view.addObject("manhole", manhole);
        view.addObject("pipes", pipes);
        view.addObject("operators", operators);
        view.addObject("path", mypath);
        return view;
    }

    /**
     * 浏览数据
     */
    @RequestMapping(value = "/findinfo")
    public ModelAndView findInfo(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) AppUtils.findMap("user");
        Manhole manhole = manholeBiz.findInfoManhole(id, null);
        if (id != 0 && StringUtils.isEmpty(manhole))
            return view;
        List<Operator> operators = operatorBiz.findListOperator(user.getCompany());
        List<Pipe> pipes = pipeBiz.findListPipe(manhole);
        view.setViewName("manhole/findinfo");
        view.addObject("manhole", manhole);
        view.addObject("pipes", pipes);
        view.addObject("operators", operators);
        view.addObject("path", mypath);
        return view;
    }

    /**
     * 浏览数据
     */
    @RequestMapping(value = "/checkview")
    public ModelAndView checkView(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) AppUtils.findMap("user");
        Manhole manhole = manholeBiz.findInfoManhole(id, null);
        if (id != 0 && StringUtils.isEmpty(manhole))
            return view;
        List<Operator> operators = operatorBiz.findListOperator(user.getCompany());
        List<Pipe> pipes = pipeBiz.findListPipe(manhole);
        view.setViewName("manhole/checkview");
        view.addObject("manhole", manhole);
        view.addObject("pipes", pipes);
        view.addObject("operators", operators);
        view.addObject("path", mypath);
        return view;
    }

    @RequestMapping(value = "/insert")
    public ModelAndView insert(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) AppUtils.findMap("user");
        Project project = projectBiz.findInfoProject(id, user);
        if (StringUtils.isEmpty(project))
            return view;
        List<Operator> operators = operatorBiz.findListOperator(user.getCompany());
        Manhole manhole = new Manhole();
        Manhole temp = manholeBiz.findLastManhole(project);
        if (StringUtils.isEmpty(temp)) {
            manhole.setProjectno(project.getName());
            manhole.setSurveyname(project.getOperator());
            manhole.setSurveydate(project.getDatetime1());
        } else {
            manhole.setAreacode(temp.getAreacode());
            manhole.setSurveyname(temp.getSurveyname());
            manhole.setSurveydate(temp.getSurveydate());
            manhole.setLocation(temp.getLocation());
            manhole.setProjectno(temp.getProjectno());
            manhole.setWorkorder(temp.getWorkorder());
            manhole.setYearlaid(temp.getYearlaid());
            manhole.setStatus(temp.getStatus());
            manhole.setNodetype(temp.getNodetype());
            manhole.setDuties(temp.getDuties());
        }
        manhole.setNode("N/A");
        manhole.setManholeid("N/A");
        manhole.setSlopeno("N/A");
        manhole.setConstruct("I");

        manhole.setCoverdislocation("N");
        manhole.setCoverchain("N");
        manhole.setScrewdamage("N");
        manhole.setScrewlose("N");
        manhole.setPlatedamage("N");
        manhole.setPlatelose("N");
        manhole.setPlateincorrect("N");
        manhole.setPlatenumber("N");
        manhole.setStairsdamage("N");
        manhole.setPlatformdamage("N");
        manhole.setOintment("N");
        manhole.setFaeces("N");
        manhole.setSewage("N");
        manhole.setSmell("N");
        manhole.setConnect("N");
        manhole.setFluide("N");
        manhole.setGas("N");
        manhole.setDangers("N");
        manhole.setVisitation("N");
        manhole.setSupplement("N");

        manhole.setCond("N");
        manhole.setCrit("N");
        manhole.setCtype("N,N,N,N,N");
        manhole.setWith1("Y");
        manhole.setWith2("Y");
        manhole.setWith3("Y");
        manhole.setProject(project);
        view.setViewName("manhole/editinfo");
        view.addObject("manhole", manhole);
        view.addObject("operators", operators);
        view.addObject("path", mypath);
        return view;
    }

    /**
     * 保存数据
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update(Manhole manhole, String type) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) AppUtils.findMap("user");
        int id = manholeBiz.replacManhole(manhole, user);
        if ("next".equals(type))
            view.setViewName("redirect:/item/editinfo?id=" + id);
        else
            view.setViewName("redirect:editinfo?id=" + id);
        return view;
    }

    /**
     * 保存数据
     */
    @RequestMapping(value = "/commit", method = RequestMethod.POST)
    public ModelAndView commit(Manhole manhole, String type) {
        ModelAndView view = new ModelAndView("common/failure");
        User user = (User) AppUtils.findMap("user");
        manholeBiz.checkManhole(manhole, user);  // 检测
        int id = manholeBiz.replacManhole(manhole, user);
        if ("next".equals(type))
            view.setViewName("redirect:/item/checkview?id=" + id);
        else
            view.setViewName("redirect:checkview?id=" + id);
        return view;
    }


    /**
     * 删除项目
     */
    @RequestMapping(value = "/delete")
    public boolean delete(@RequestParam(defaultValue = "0") int id) {
        User user = (User) AppUtils.findMap("user");
        Manhole manhole = manholeBiz.findInfoManhole(id, user);
        if (!StringUtils.isEmpty(manhole))
            manholeBiz.deleteManhole(manhole);
        return true;
    }

    /**
     * 移除项目
     */
    @RequestMapping(value = "/remove")
    public boolean remove(@RequestParam(defaultValue = "0") int id) {
        User user = (User) AppUtils.findMap("user");
        Map<String, Object> map = AppUtils.getMap("id", id, "company", user.getCompany());
        Manhole manhole = manholeBiz.findInfoManhole(map);
        if (!StringUtils.isEmpty(manhole))
            manholeBiz.deleteManhole(manhole);
        return true;
    }

}
